package org.hncors.gnssparse.fragement;

import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.CoordinateConverter;
import com.southgnss.connect.DeviceParManage;
import com.southgnss.connect.GnssInfoCallBackLinstner;
import com.southgnss.connect.TopDeviceManage;
import com.southgnss.location.DateUtil;

import org.hncors.gnssparse.R;
import org.hncors.gnssparse.activity.LocationListActivity;
import org.hncors.gnssparse.base.BaseFragment;
import org.hncors.gnssparse.base.MyApplication;
import org.hncors.gnssparse.model.LocationItemData;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.Locale;

import static android.content.Context.SENSOR_SERVICE;

public class HomeFragment extends BaseFragment implements View.OnClickListener, GnssInfoCallBackLinstner, SensorEventListener {
    private View view;
    // 定位相关
    LocationClient mLocClient;
    public MyLocationListener myListener = new MyLocationListener();
    private MyLocationConfiguration.LocationMode mCurrentMode;
    BitmapDescriptor mCurrentMarker;
    private static final int accuracyCircleFillColor = 0xAAFFFF88;
    private static final int accuracyCircleStrokeColor = 0xAA00FF00;
    private SensorManager mSensorManager;
    private Double lastX = 0.0;
    private int mCurrentDirection = 0;
    private double mCurrentLat = 0.0;
    private double mCurrentLon = 0.0;
    private float mCurrentAccracy;

    MapView mMapView;
    BaiduMap mBaiduMap;

    // UI相关
    RadioGroup.OnCheckedChangeListener radioButtonListener;
    Button requestLocButton;
    boolean isFirstLoc = true; // 是否首次定位
    private MyLocationData locData;
    private float direction;

    private Button mapTypeButton;


    private TextView nameTv;

    private TextView bValueTv;

    private TextView lValueTv;

    private TextView hValueTv;

    private TextView timeTv;

    private TextView precisionTv;

    private TextView speedTv;

    private ImageButton topRightButton;
    private Button controllButton;

    private boolean isGranter = false;
    private final int REQUEST_PHONE_PERMISSIONS = 0;
    private boolean isRunning = true;
    private Marker gnssMarker;
    static Handler mHandler = new Handler();
    Runnable onTimerRunnable = new Runnable() {
        @Override
        public void run() {
            String strInfo = String.format(Locale.ENGLISH, "%s:%.10f%n %s:%.10f%n %s:%.4f%n %s:%.4f%n %s:%.4f%n " +
                            "%s:%.4f%n %s:%.4f%n %s:%.4f%n %s:%.4f%n %s:%.4f%n %s:%s%n %s:%s%n %s:%d%n %s:%d%n " +
                            "%s:%s%n %s:%s%n %s:%s%n %s:%s%n "

                    , getString(R.string.B), TopDeviceManage.getInstance(null).getLatitude()
                    , getString(R.string.L), TopDeviceManage.getInstance(null).getLongitude()
                    , getString(R.string.H), TopDeviceManage.getInstance(null).getAltitude()
                    , getString(R.string.HRMS), TopDeviceManage.getInstance(null).getHrms()
                    , getString(R.string.speed), TopDeviceManage.getInstance(null).getSpeed()
                    , getString(R.string.heading), TopDeviceManage.getInstance(null).getBearing()
                    , getString(R.string.PDOP), TopDeviceManage.getInstance(null).getPdop()
                    , getString(R.string.HDOP), TopDeviceManage.getInstance(null).getHdop()
                    , getString(R.string.VDOP), TopDeviceManage.getInstance(null).getVdop()
                    , getString(R.string.VRMS), TopDeviceManage.getInstance(null).getVrms()
                    , getString(R.string.statusType), getCurSolutionState(TopDeviceManage.getInstance(null).getStatusType())
                    , getString(R.string.time), DateUtil.formatDate(new Date(TopDeviceManage.getInstance(null).getLocalTime()))
                    , getString(R.string.age), TopDeviceManage.getInstance(null).getAge()
                    , getString(R.string.m_tSatNum), TopDeviceManage.getInstance(null).getSatInView()
                    , "固件版本", DeviceParManage.GetInstance().GetRTKAppVer()
                    , "工作模式", DeviceParManage.GetInstance().GetCurSysMode()
                    , "电池剩余", DeviceParManage.GetInstance().getPowerFree().isEmpty() ? "" : (DeviceParManage.GetInstance().getPowerFree() + "%")
                    , "差分站ID", TopDeviceManage.getInstance(null).getChangeStationID()
            );
            // tvGnssInfo.setText(strInfo);

            // tvGnssInfo.setText(strInfo);

            try {
                if (isRunning) {
                    refreshUI(strInfo);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            mHandler.postDelayed(onTimerRunnable, 1000);
        }
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        //定义了一个地图view
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(mActivity.getApplicationContext());
        SDKInitializer.setCoordType(CoordType.GCJ02);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(view == null){
            view = super.onCreateView(inflater,container,savedInstanceState);
            appendTopBody(R.layout.activity_top_icon);
            appendMainBody(this, R.layout.activity_map);
            setTopBarTitle("问北咫尺Demo");
            topRightButton = (ImageButton) view.findViewById(R.id.top_right_icon);
            topRightButton.setBackground(getResources().getDrawable(R.drawable.icon_message));
            topRightButton.setVisibility(View.VISIBLE);
            topRightButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent();
                    intent.setClass(mActivity, LocationListActivity.class);
                    startActivity(intent);
                }
            });
        }
        initData();
        initView();
        initMap();
        return view;
    }
    private void initData() {
        TopDeviceManage.getInstance(mActivity).setGnssInfoListener(this);
    }
    private void initView() {
        mMapView = (MapView) view.findViewById(R.id.mmap);
        mapTypeButton = (Button) view.findViewById(R.id.map_type_button);
        nameTv = (TextView) view.findViewById(R.id.name_tv);
        bValueTv = (TextView) view.findViewById(R.id.b_value_tv);
        lValueTv = (TextView) view.findViewById(R.id.l_value_tv);
        hValueTv = (TextView) view.findViewById(R.id.h_value_tv);
        timeTv = (TextView) view.findViewById(R.id.time_tv);
        precisionTv = (TextView) view.findViewById(R.id.precision_tv);
        speedTv = (TextView) view.findViewById(R.id.speed_tv);
        controllButton = (Button) view.findViewById(R.id.control_button);
        mBaiduMap = mMapView.getMap();
        mHandler.post(onTimerRunnable);

        mapTypeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mapTypeButton.isSelected()) {
                    mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
                } else {
                    mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
                }
                mapTypeButton.setSelected(!mapTypeButton.isSelected());
            }
        });
        controllButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (controllButton.isSelected()) {
                    controllButton.setText("暂停");
                    isRunning = true;
                } else {
                    controllButton.setText("开始");
                    isRunning = false;
                }
                controllButton.setSelected(!controllButton.isSelected());
            }
        });
    }
    private void initMap() {
        mSensorManager = (SensorManager) mActivity.getSystemService(SENSOR_SERVICE);//获取传感器管理服务
        mCurrentMode = MyLocationConfiguration.LocationMode.FOLLOWING;
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);

        // 定位初始化
        mLocClient = new LocationClient(mActivity);
        mLocClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
        mLocClient.setLocOption(option);
        mLocClient.start();
        mLocClient.requestLocation();
    }
    @Override
    public void onPause() {
        mMapView.onPause();
        super.onPause();
    }

    @Override
    public void onResume() {
        mMapView.onResume();
        super.onResume();
        //为系统的方向传感器注册监听器
        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_UI);

    }

    @Override
    public void onStop() {
        //取消注册传感器监听
        mSensorManager.unregisterListener(this);
        super.onStop();
    }

    @Override
    public void onDestroy() {
        // 退出时销毁定位
        mLocClient.stop();
        // 关闭定位图层
        mBaiduMap.setMyLocationEnabled(false);
        mMapView.onDestroy();
        mMapView = null;
        super.onDestroy();
    }


    private void refreshUI(String strInfo) {
        TopDeviceManage extras = TopDeviceManage.getInstance(null);
        DecimalFormat nf = new DecimalFormat("#0.0000");
        DecimalFormat speedDf = new DecimalFormat("#0.00");
        TopDeviceManage topDeviceManage = TopDeviceManage.getInstance(null);
        nameTv.setText(DateUtil.formatDate(new Date(TopDeviceManage.getInstance(null).getLocalTime())));
        bValueTv.setText(nf.format(topDeviceManage.getLatitude()));
        lValueTv.setText(nf.format(topDeviceManage.getLongitude()));
        hValueTv.setText(nf.format(topDeviceManage.getAltitude()));
        timeTv.setText(TopDeviceManage.getInstance(null).getSatInView() + "");
        precisionTv.setText(getCurSolutionState(TopDeviceManage.getInstance(null).getStatusType()));
        speedTv.setText(nf.format(TopDeviceManage.getInstance(null).getPdop()));
        refreshMarker(topDeviceManage.getLatitude(), topDeviceManage.getLongitude());


        LocationItemData item = new LocationItemData();
        // 卫星列表
        item.bValue =nf.format(topDeviceManage.getLatitude());
        item.lValue = nf.format(topDeviceManage.getLongitude());
        item.hValue = nf.format(topDeviceManage.getAltitude());
        item.timeStr = DateUtil.formatDate(new Date(extras.getLocalTime()));
        item.mSatelliteStatus = getCurSolutionState(extras.getStatusType());
        item.strInfo = strInfo;
        if (extras.getLatitude() > 0 && extras.getLongitude() > 0) {
            MyApplication.getInstance().mSateItemsDataArray.add(item);
//            mSatelliteItemAdapter.notifyDataSetChanged();
//            mFileLogger.onNmeaReceived(System.currentTimeMillis(), strInfo);
        }
    }
    private void refreshMarker(Double lat, Double lng) {
        if (lat > 0 && lng > 0) {
            LatLng sourceLatLng = new LatLng(lat, lng);
            CoordinateConverter converter  = new CoordinateConverter();
            converter.from(CoordinateConverter.CoordType.GPS);

// sourceLatLng待转换坐标
            converter.coord(sourceLatLng);
            LatLng point = converter.convert();
            //构建Marker图标

            if (gnssMarker == null) {
                BitmapDescriptor bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.icon_trimble_normal);
                OverlayOptions option = new MarkerOptions()
                        .icon(bitmap)
                        .position(point);
                mBaiduMap.addOverlay(option);
            } else {
                gnssMarker.setPosition(point);
            }
            MapStatus.Builder builder = new MapStatus.Builder();
            builder.target(point).zoom(16.0f);
            mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
        }
    }
    private String getCurSolutionState(int statusType) {
        String statusTypeString = "";
        switch (statusType) {
            case 0:// 无效定位
                break;
            case 1:// 单点
                statusTypeString = "单点";
                break;
            case 2:// 差分
            case 3:
                statusTypeString = "差分";
                break;
            case 4:// 固定
                statusTypeString = "固定";
                break;
            case 8:
                statusTypeString = "固定";
                break;
            case 5:// 浮动
                statusTypeString = "浮动";
                break;
            default:
                break;
        }
        return statusTypeString;
    }
    @Override
    public void onClick(View v) {

    }

    @Override
    public void callback(String s) {

    }

    @Override
    public void connectCallback(boolean b) {

    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        double x = sensorEvent.values[SensorManager.DATA_X];
        if (Math.abs(x - lastX) > 1.0) {
            mCurrentDirection = (int) x;
            locData = new MyLocationData.Builder()
                    .accuracy(mCurrentAccracy)
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(mCurrentDirection).latitude(mCurrentLat)
                    .longitude(mCurrentLon).build();
            mBaiduMap.setMyLocationData(locData);
        }
        lastX = x;
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }

    /**
     * 定位SDK监听函数
     */
//    public class MyLocationListenner implements BDAbstractLocationListener {
//
//        @Override
//        public void onReceiveLocation(BDLocation location) {
//            // map view 销毁后不在处理新接收的位置
//            if (location == null || mMapView == null) {
//                return;
//            }
//            mCurrentLat = location.getLatitude();
//            mCurrentLon = location.getLongitude();
//            mCurrentAccracy = location.getRadius();
//            locData = new MyLocationData.Builder()
//                    .accuracy(location.getRadius())
//                    // 此处设置开发者获取到的方向信息，顺时针0-360
//                    .direction(mCurrentDirection).latitude(location.getLatitude())
//                    .longitude(location.getLongitude()).build();
//            mBaiduMap.setMyLocationData(locData);
//            if (isFirstLoc) {
//                isFirstLoc = false;
//                LatLng ll = new LatLng(location.getLatitude(),
//                        location.getLongitude());
//                MapStatus.Builder builder = new MapStatus.Builder();
//                builder.target(ll).zoom(16.0f);
//                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
//            }
//        }
//
//        public void onReceivePoi(BDLocation poiLocation) {
//        }
//    }
    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location){
            //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
            //以下只列举部分获取经纬度相关（常用）的结果信息
            //更多结果信息获取说明，请参照类参考中BDLocation类中的说明

//            double latitude = location.getLatitude();    //获取纬度信息
//            double longitude = location.getLongitude();    //获取经度信息
//            float radius = location.getRadius();    //获取定位精度，默认值为0.0f
//
//            String coorType = location.getCoorType();
//            //获取经纬度坐标类型，以LocationClientOption中设置过的坐标类型为准
//
//            int errorCode = location.getLocType();
            //获取定位类型、定位错误返回码，具体信息可参照类参考中BDLocation类中的说明

            mCurrentLat = location.getLatitude();
            mCurrentLon = location.getLongitude();
            mCurrentAccracy = location.getRadius();
            locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(mCurrentDirection).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
            if (isFirstLoc) {
                isFirstLoc = false;
                LatLng ll = new LatLng(location.getLatitude(),
                        location.getLongitude());
                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(ll).zoom(16.0f);
                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
            }
        }
    }

}
